<?php
defined('BASEPATH') OR exit('No direct script access allowed');

// 已废弃，查看新版haozhuanyev2
class Haozhuanye {

    // 接口域名
    private $hostName = 'http://connect.nicezhuanye.com/';
    
    // 接口地址
    private $apiUrl = "http://connect.nicezhuanye.com/schoolscheduleserv/integration/";
    
    // ticket地址
    private $ticketUrl = "http://connect.nicezhuanye.com/nicezhuanye/ssoLogin/ticket?ticket=";

	public function __construct() {
        // if(ENV == 'test') {
        //     // 本地环境
        //     $this->hostName = 'https://changheng.hqjltech.com/';
        //     $this->apiUrl = 'https://changheng.hqjltech.com/dataconnectorserv/integration/';
        // }
        // if(ENVIRONMENT == 'development') {
        //     // 测试环境
        //     $this->hostName = 'https://changheng.hqjltech.com/';
        //     $this->apiUrl = 'https://changheng.hqjltech.com/dataconnectorserv/integration/';
        // }
    }

    // 获取学校信息
    public function get_school_info($schoolId, $loginId, $token){
        $url = $this->apiUrl. "scheduler/getSchoolInfo";
        $postData['schoolID'] = $schoolId;
        $postData['login'] = $loginId;
        $postData['token'] = $token;

        $postData = json_encode($postData);
        $result = $this->post_haozhuanye($url, $postData);
        return json_decode($result, true);
    }

    // 获取教师列表
    public function get_school_teacher($schoolId, $loginId, $token){
        $url = $this->apiUrl. "scheduler/getTeachers";
        $postData['schoolID'] = $schoolId;
        $postData['login'] = $loginId;
        $postData['token'] = $token;
        
        $postData = json_encode($postData);
        $result = $this->post_haozhuanye($url, $postData);
        return json_decode($result, true);
    }

    // 获取最新科目列表
    public function get_school_lesson($schoolId, $loginId, $token){
        $url = $this->apiUrl. "scheduler/getSubjects";
        $postData['schoolID'] = $schoolId;
        $postData['login'] = $loginId;
        $postData['token'] = $token;
        
        $postData = json_encode($postData);
        $result = $this->post_haozhuanye($url, $postData);
        return json_decode($result, true);
    }

    // 获取全部位置信息接口
    public function get_school_classroom($schoolId, $loginId, $token){
        $url = $this->apiUrl. "scheduler/getLocations";
        $postData['schoolID'] = $schoolId;
        $postData['login'] = $loginId;
        $postData['token'] = $token;
        
        $postData = json_encode($postData);
        $result = $this->post_haozhuanye($url, $postData);
        return json_decode($result, true);
    }

    // 获取行政班信息
    public function get_school_classes($schoolId, $loginId, $token){
        $url = $this->apiUrl. "scheduler/getClasses";
        $postData['schoolID'] = $schoolId;
        $postData['login'] = $loginId;
        $postData['token'] = $token;
        
        $postData = json_encode($postData);
        $result = $this->post_haozhuanye($url, $postData);
        return json_decode($result, true);
    }
    // 获取教学班信息
    public function get_school_teaching_classes($schoolId, $loginId, $token){
        $url = $this->apiUrl. "scheduler/getTeachingClasses";
        $postData['schoolID'] = $schoolId;
        $postData['login'] = $loginId;
        $postData['token'] = $token;
        
        $postData = json_encode($postData);
        $result = $this->post_haozhuanye($url, $postData);
        return json_decode($result, true);
    }
    // 获取学生信息
    public function get_school_student($schoolId, $loginId, $token){
        $url = $this->apiUrl. "scheduler/getStudentInfos";
        $postData['schoolID'] = $schoolId;
        $postData['login'] = $loginId;
        $postData['token'] = $token;
        
        $postData = json_encode($postData);
        $result = $this->post_haozhuanye($url, $postData);
        return json_decode($result, true);
    }

    // 获取学生关系信息
    /*
    public function get_school_student_relation($schoolId, $loginId, $token){
        $url = $this->apiUrl. "scheduler/getStudentClasses";
        $postData['schoolID'] = $schoolId;
        $postData['login'] = $loginId;
        $postData['token'] = $token;
        
        $postData = json_encode($postData);
        $result = $this->post_haozhuanye($url, $postData);
        return json_decode($result, true);
    }
    */

    // 获取本周课表接口
    public function get_school_schedule($schoolId, $loginId, $token){
        $url = $this->apiUrl. "scheduler/getSchedule";
        // $url = $this->apiUrl. "scheduler/getNextSchedule";
        $postData['schoolID'] = $schoolId;
        $postData['login'] = $loginId;
        $postData['token'] = $token;
        
        $postData = json_encode($postData);
        $result = $this->post_haozhuanye($url, $postData);
        return json_decode($result, true);
    }
    // 获取下周课表接口
    public function get_school_next_schedule($schoolId, $loginId, $token){
        $url = $this->apiUrl. "scheduler/getNextSchedule";
        $postData['schoolID'] = $schoolId;
        $postData['login'] = $loginId;
        $postData['token'] = $token;
        
        $postData = json_encode($postData);
        $result = $this->post_haozhuanye($url, $postData);
        return json_decode($result, true);
    }

    // 提交考勤数据
    public function post_sign_data($postData){
        $url = $this->apiUrl. "classadmin/loadCheckings";
        
        $postData = json_encode($postData);
        $result = $this->post_haozhuanye($url, $postData);
        return json_decode($result, true);
    }

    // 校验ticket
    public function check_hzy_ticket($ticket) {
        $result = file_get_contents($this->ticketUrl. $ticket);
        return $result;
    }

    // 获取考试安排数据
    public function get_school_exam($schoolId){
        $url = $this->hostName . "examschedulerserv/exam/getExamPublishList";
        $postData['parameter']['schoolID'] = $schoolId;
        $postData = json_encode($postData);
        $result = $this->post_haozhuanye($url, $postData);
        return json_decode($result, true);
    }

    // 获取学生选科的请求url
    public function get_subject_choice_report($schoolId, $loginId, $token, $xStudentId) {
        $url = $this->hostName . "nicezhuanye/share/getStuSubjChoiceReport";
        $postData = array(
            'schoolID' => $schoolId,
            'login' => $loginId,
            'token' => $token,
            'studentEID' => $xStudentId,
        );
        $postData = json_encode($postData);
        $result = $this->post_haozhuanye($url, $postData);
        return json_decode($result, true);
    }

    // 获取选修班信息
    public function get_school_elective_classes($schoolId, $loginId, $token){
        $url = $this->apiUrl. "scheduler/getElectiveClasses";
        $postData['schoolID'] = $schoolId;
        $postData['login'] = $loginId;
        $postData['token'] = $token;
        
        $postData = json_encode($postData);
        $result = $this->post_haozhuanye($url, $postData);
        return json_decode($result, true);
    }

    // 获取‘我的考勤’地址
    public function get_student_attendance_url() {
        return $this->hostName . "login/wechatlogincheck.html?service=mixiao&jumpUrl=" . $this->hostName . "classadminstu/Student&ticket=";
    }

    // 提交数据到服务器
    private function post_haozhuanye($url, $data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // 发送json类型的header
        $header = array("Content-Type: application/json");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $tmpInfo = curl_exec($ch);

        if(curl_errno($ch)) return curl_error($ch);
        curl_close($ch);
        return $tmpInfo;
    }
}
